package com.dragon.concurrent.demo.interrupt;

/**
 * 当线程处于等待或者定时等待,也是就是当前线程sleep(),wait()方法时,如果线程被别的线程打断,则直接会抛出InterruptedException
 * 可以看出 sleep() 方法被中断后会清除中断标记，所以循环会继续运行。。
 *
 * @author dragon
 * @version V1.0.0-RELEASE
 * @date 2019/6/1 18:42
 */
public class ThreadInterruptTest3 {

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            while (true) {
                // 响应中断
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("Java技术栈线程被中断，程序退出。");
                    return;
                }
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    System.out.println("Java技术栈线程休眠被中断，程序退出。");
                }
            }
        });
        thread.start();
        Thread.sleep(1000);
        thread.interrupt();
        System.out.println("main主线程运行结束");
    }
}
